from fastapi import Query
from pydantic import BaseModel, Field
from typing import Any, Optional, Generic, Sequence, TypeVar
from datetime import datetime
from fastapi_pagination.bases import AbstractParams, AbstractPage, RawParams
import pydantic

T = TypeVar("T")


class BaseSchema(BaseModel):
    pass
    # remark: Optional[str] = Field(description="备注")


class RecodeMixinSchema():
    id: int | None = Field(default=None, description="ID")
    created_time: datetime | None = Field(description="创建时间")
    created_by: str | None = Field(default=None, description="创建者")
    updated_time: datetime | None = Field(description="更新时间")
    updated_by: str | None = Field(default=None, description="更新者")
    deleted_time: datetime | None = Field(description="删除时间")
    deleted_by: str | None = Field(default=None, description="删除者")
    is_deleted: int | None = Field(default=None, description="是否删除")
# class RecodeMixinSchema():
#     id: int = Field(None, description="ID")
#     created_time: Optional[datetime] = Field(
#         description="创建时间")
#     created_by: Optional[str] = Field(
#         None,
#         description="创建者"
#     )  # 暂时设置为 可选参数 optional 需要在orm中自动提取jwt中的user
#     updated_time: Optional[datetime] = Field(
#         description="更新时间")
#     updated_by: str | None = Field(
#         default=None,
#         description="更新者"
#     )  # 暂时设置为 可选参数 optional 需要在orm中自动提取jwt中的user
#     deleted_time: Optional[datetime] = Field(

#         description="删除时间")
#     deleted_by: Optional[str] = Field(None, description="删除者")
#     is_deleted: Optional[int] = Field(None, description="是否删除")


class UnifiedResponse(BaseModel):
    msg: str
    data: Any
    code: int


class PageParams(BaseModel, AbstractParams):
    page: int = Query(1, ge=1, description='Page Number')
    size: int = Query(20, gt=0, le=60, description='Page Size')

    def to_raw_params(self) -> RawParams:
        offset = (self.page - 1) * self.size
        return RawParams(limit=self.size, offset=offset)


class PageInationResult(AbstractPage[T], Generic[T]):
    """
    分页结果封装
        items: 返回集列表
        total: 结果总数
    """
    total: int
    page: int
    size: int
    list: Sequence[T]

    __params_type__ = PageParams

    @classmethod
    def create(cls, items: Sequence[T], total: int, params: PageParams):
        return cls(list=items, total=total, page=params.page, size=params.size)
